<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 课程分类控制器
 */
class Order extends Backend
{
    /**
     * 当前控制器下的一个模型属性
     */
    protected $relationSearch = true;
    protected $searchFields = ['total'];
    // 初始化
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->SubjectOrderModel = model('Subject.Order');

        // $this->rulelist = $this->CategoryModel->select();
        // foreach ($this->rulelist as $k => &$v) {

        //     $ruledata[$v['id']] = $v['name'];
        // }
        // $this->view->assign('ruledata', $ruledata);
    }



    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
            $this->changeWhereName("id", "order.id");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SubjectOrderModel
                ->with(['subject','business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数
               
            $list = $this->SubjectOrderModel
                ->with(['subject','business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据
            // echo   $this->SubjectOrderModel->getlastSql();exit;
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->SubjectOrderModel->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('没有找到该课程'));
                exit;
            }
        }

        $result = $this->SubjectOrderModel->destroy($ids);

        if ($result === false) {
            $this->error($this->SubjectOrderModel->getError());
            exit;
        }

        $this->success();
        exit;

    }

}
